<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import serviceManager from './services/serviceManager.js'

export default {
  name: 'App',
  setup() {
    const router = useRouter()
    
    const deviceStatus = reactive({
      online: true,
      deviceId: 'MOBOX-****-****-****'
    })
    
    // 获取真实设备ID
    const fetchDeviceId = async () => {
      try {
        const res = await axios.get('/api/system/info')
        if (res.data.status === 'success') {
          deviceStatus.deviceId = res.data.data.box_id
          deviceStatus.online = true
        }
      } catch (error) {
        console.error('获取设备ID失败:', error)
        deviceStatus.online = false
      }
    }
    
    const handleMenuSelect = (index) => {
      console.log('导航到:', index)
      router.push(index)
    }
    
    const refreshData = async () => {
      try {
        // 刷新所有服务
        await serviceManager.refreshAllServices()
        
        // 重新获取设备信息
        await fetchDeviceId()
        
        ElMessage.success('数据刷新成功')
      } catch (error) {
        console.error('刷新失败:', error)
        ElMessage.error('数据刷新失败')
      }
    }
    
    onMounted(async () => {
      console.log('🚀 MoBox Dashboard 启动成功（优化版）')
      console.log('当前路由:', router.currentRoute.value.path)
      
      // 获取设备ID
      await fetchDeviceId()
      
      // 初始化当前页面的服务
      const currentPath = router.currentRoute.value.path
      await serviceManager.enterPage(currentPath)
    })
    
    return {
      deviceStatus,
      handleMenuSelect,
      refreshData
    }
  }
}
</script>

<template>
  <div id="app">
    <div class="layout-container">
      <!-- 专业侧边栏 -->
      <div class="sidebar">
        <div class="logo">
          <div class="logo-icon">🚀</div>
          <div class="logo-text">
            <h2>MoBox Dashboard</h2>
            <p class="subtitle">单板机管理系统</p>
          </div>
        </div>
        
        <el-menu
          :default-active="$route.path"
          class="sidebar-menu"
          @select="handleMenuSelect"
          background-color="transparent"
          text-color="#ecf0f1"
          active-text-color="#ffffff"
        >
          <el-menu-item index="/" class="menu-item">
            <el-icon><House /></el-icon>
            <span>设备概览</span>
          </el-menu-item>
          
          <el-menu-item index="/monitor" class="menu-item">
            <el-icon><PieChart /></el-icon>
            <span>系统监控</span>
          </el-menu-item>
          
          <el-menu-item index="/network" class="menu-item">
            <el-icon><Link /></el-icon>
            <span>网络配置</span>
          </el-menu-item>
          
          <el-menu-item index="/docker" class="menu-item">
            <el-icon><Box /></el-icon>
            <span>Docker管理</span>
          </el-menu-item>
          
          <el-menu-item index="/experiment" class="menu-item">
            <el-icon><Operation /></el-icon>
            <span>实验管理</span>
          </el-menu-item>
          
          <el-menu-item index="/settings" class="menu-item">
            <el-icon><Setting /></el-icon>
            <span>设备设置</span>
          </el-menu-item>
        </el-menu>
        
        <!-- 设备状态 -->
        <div class="device-status">
          <el-card class="status-card" shadow="never">
            <div class="status-indicator">
              <span class="status-dot online"></span>
              <span class="status-text">设备在线</span>
            </div>
            <div class="device-id">
              <el-icon><Star /></el-icon>
              <span>{{ deviceStatus.deviceId }}</span>
            </div>
          </el-card>
        </div>
      </div>
      
      <!-- 右侧主容器 -->
      <div class="main-container">
        <!-- 顶部导航栏 -->
        <div class="header">
          <div class="header-left">
            <h3>{{ $route.meta.title || 'MoBox Dashboard' }}</h3>
            <p>专业单板机管理平台</p>
          </div>
          <div class="header-right">
            <el-button type="primary" @click="refreshData">
              <el-icon><Refresh /></el-icon>
              刷新
            </el-button>
          </div>
        </div>
        
        <!-- 内容区域 -->
        <div class="main-content">
          <router-view />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 确保完整布局 */
#app {
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.layout-container {
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  flex-direction: row;
  margin: 0;
  padding: 0;
}

/* 侧边栏样式 - 固定宽度 */
.sidebar {
  width: 280px;
  min-width: 280px;
  max-width: 280px;
  flex-shrink: 0;
  background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  height: 100vh;
  overflow: hidden;
  position: relative;
}

/* 右侧主容器 - 占用剩余空间 */
.main-container {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* 顶部导航栏 */
.header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 30px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  height: 80px;
  min-height: 80px;
  flex-shrink: 0;
}

/* 主内容区域 - 占用剩余高度 */
.main-content {
  flex: 1;
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  overflow-y: auto;
  width: 100%;
  min-height: 0;
  box-sizing: border-box;
}

.logo {
  padding: 30px 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
}

.logo-icon {
  font-size: 36px;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-10px); }
}

.logo-text h2 {
  margin: 0;
  color: #ffffff;
  font-size: 20px;
  font-weight: 600;
}

.subtitle {
  margin: 5px 0 0 0;
  font-size: 12px;
  color: #bdc3c7;
  opacity: 0.8;
}

/* 菜单样式 */
.sidebar-menu {
  border: none;
  padding: 20px 0;
  height: calc(100vh - 200px);
  overflow-y: auto;
}

.menu-item {
  margin: 8px 16px;
  border-radius: 12px;
  transition: all 0.3s ease;
  min-height: 56px;
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.1) !important;
  transform: translateX(8px);
}

.menu-item.is-active {
  background: linear-gradient(135deg, #3498db, #2980b9) !important;
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
  color: #ffffff !important;
}

.menu-item .el-icon {
  margin-right: 12px;
  font-size: 18px;
  width: 18px;
  height: 18px;
}

.menu-item span {
  font-size: 15px;
  font-weight: 500;
}

/* 设备状态 */
.device-status {
  position: absolute;
  bottom: 20px;
  left: 20px;
  right: 20px;
}

.status-card {
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  backdrop-filter: blur(10px);
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.status-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background-color: #27ae60;
  box-shadow: 0 0 0 4px rgba(39, 174, 96, 0.2);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(39, 174, 96, 0.7); }
  70% { box-shadow: 0 0 0 10px rgba(39, 174, 96, 0); }
  100% { box-shadow: 0 0 0 0 rgba(39, 174, 96, 0); }
}

.status-text {
  color: #ecf0f1;
  font-size: 14px;
  font-weight: 500;
}

.device-id {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #bdc3c7;
  font-size: 12px;
  font-family: 'JetBrains Mono', monospace;
}

.header-left h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 24px;
  font-weight: 600;
}

.header-left p {
  margin: 4px 0 0 0;
  color: #7f8c8d;
  font-size: 14px;
}

/* Element Plus 覆盖样式 */
:deep(.el-container) {
  height: auto !important;
  display: block !important;
}

:deep(.el-aside) {
  width: auto !important;
  height: auto !important;
}

:deep(.el-main) {
  padding: 0 !important;
  width: 100% !important;
  height: 100% !important;
}

:deep(.el-header) {
  padding: 0 !important;
  height: auto !important;
}

/* Element Plus 菜单项覆盖样式 */
:deep(.el-menu-item) {
  color: #ecf0f1 !important;
}

:deep(.el-menu-item.is-active) {
  background: linear-gradient(135deg, #3498db, #2980b9) !important;
  color: #ffffff !important;
}

:deep(.el-menu-item:hover) {
  background: rgba(255, 255, 255, 0.1) !important;
  color: #ffffff !important;
}
</style>
